﻿module Sustainalytics.News {

    export class NewsRegionService implements INewsRegionService {

        private $q: ng.IQService;
        private items: NewsRegion[];

        constructor($q: ng.IQService) {
            this.$q = $q;

            this.items =
            [
                { id: 1, name: 'American' },
                { id: 2, name: 'Canadian' },
                { id: 3, name: 'Dutch' },
                { id: 4, name: 'German' },
                { id: 5, name: 'Global' },
                { id: 6, name: 'Other' },
                { id: 7, name: 'Spanish' }
            ];
        }

        // get news region by ID
        public getNewsRegion(id: number): ng.IPromise<NewsRegion> {
            var def: ng.IDeferred<NewsRegion> = this.$q.defer();

            var item: NewsRegion = null;
            $(this.items).each(function () {
                if (this.id == id) {
                    item = this;
                    return false;
                }
            });

            def.resolve(item);

            return def.promise;
        }

        // get news regions
        public getNewsRegions(): ng.IPromise<NewsRegion[]> {
            var def: ng.IDeferred<NewsRegion[]> = this.$q.defer();
            def.resolve(this.items);
            return def.promise;
        }
    }

    // register service with News Angular module
    angular.module('Sustainalytics.News').factory('NewsRegionService', function ($q) {
        var srv = new NewsRegionService($q);
        return srv;
    });
}